<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :sm="24" :md="24" :xxl="16" v-if="!isBusiness">
            <a-form-item label="区域">
              <a-row :gutter="5" type="flex">
                <a-col :span="3">
                  <a-select
                    placeholder="省"
                    v-model="queryParam.provinceCode"
                    @change="e => addressChange(e, 'cityOptions', 'province')"
                  >
                    <a-select-option v-for="item in provinceOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="市"
                    v-model="queryParam.cityCode"
                    @change="e => addressChange(e, 'areaOptions', 'city')"
                  >
                    <a-select-option v-for="item in cityOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="区"
                    v-model="queryParam.areaCode"
                    @change="e => addressChange(e, 'streetOptions', 'area')"
                  >
                    <a-select-option v-for="item in areaOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="街道"
                    v-model="queryParam.streetCode"
                    @change="e => addressChange(e, '', 'street')"
                  >
                    <a-select-option v-for="item in streetOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="商家"
                    v-model="queryParam.businessId"
                    @change="e => agentChange(e, 'networkOptions', 'agentCode')"
                  >
                    <a-select-option v-for="item in agentOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="网点"
                    v-model="queryParam.networkId"
                    @change="e => agentChange(e, 'deviceOptions', 'networkCode')"
                  >
                    <a-select-option v-for="item in networkOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select placeholder="设备" v-model="queryParam.deviceId">
                    <a-select-option v-for="item in deviceOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
              </a-row>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="网点名称">
              <a-input placeholder="请输入" v-model="queryParam.networkName"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus || isBusiness">
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="创建时间">
                <a-range-picker allowClear class="per100" v-model="tempTimes" @change="timeChange" />
              </a-form-item>
            </a-col>
            <!-- <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="所属公司">
                <a-input placeholder="请输入" v-model="queryParam.advertisingScreen"></a-input>
              </a-form-item>
            </a-col> -->
          </template>

          <a-col :sm="24" :md="12" :xl="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px" v-if="!isBusiness">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator mt10">
      <a-button type="success" icon="plus" v-has="'network:add'" @click="handUpdate()">新增</a-button>
    </div>

    <!-- table区域 -->
    <a-table
      ref="table"
      bordered
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :scroll="{ x: 1200 }"
      @change="handleTableChange"
    >
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" class="ml5 mr5" size="small" v-has="'network:edit'" @click="handUpdate(record.id)">编辑</a-button>
        <a-button type="danger" class="ml5 mr5" size="small" v-has="'network:delete'" @click="handDelete(record.id)">删除</a-button>
      </span>
    </a-table>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import Api from '@/api/NetworkManage/NetworkList'
import { RoleMixin } from '@/mixins/roleUtil'
import { deleteAction, getAction } from '@api/manage'

export default {
  name: 'NetworkList',
  mixins: [JeecgListMixin, RoleMixin],
  data() {
    return {
      url: {
        list: Api.list,
        deleteUrl: Api.delete
      },
      columns: [],
      provinceList: [],
      cityList: [],
      areaList: [],
      streetList: []
    }
  },
  created() {
    this.columns = [
      {
        title: '序号',
        dataIndex: 'index',
        align: 'center',
        width: 100,
        customRender: (text, record, index) => `${index + 1}`
      },
      {
        title: '网点名称',
        dataIndex: 'networkName',
        align: 'center',
        sorter: (a, b) => {
          return a.networkName - b.networkName
        }
      },
      {
        title: '商家名称',
        dataIndex: 'businessName',
        align: 'center'
      },
      {
        title: '区域',
        dataIndex: 'provinceCn',
        align: 'center',
        width: 300,
        customRender: (text, record, index) => {
          return ['provinceCn', 'cityCn', 'areaCn', 'streetCn']
            .filter(field => record[field])
            .map(field => record[field])
            .reduce((pre, cur) => pre + cur, '')
        },
        sorter: (a, b) => {
          return a.provinceCn - b.provinceCn
        }
      },
      {
        title: '服务时间',
        align: 'center',
        width: 100,
        dataIndex: 'serviceTime',
        sorter: (a, b) => {
          return a.serviceTime - b.serviceTime
        }
      },
      {
        title: '创建日期',
        align: 'center',
        dataIndex: 'createTime',
        sorter: (a, b) => {
          return a.createTime - b.createTime
        }
      },
      {
        title: '操作',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      }
    ]
    this.getAddress('provinceOptions')
  },
  methods: {
    // 新增/编辑网点
    handUpdate(id) {
      const key = id ? id : 'add'
      this.$router.push(`/networkManage/networkUpdate/${key}`)
    },

    handDelete(id) {
      if (!id) {
        return
      }
      deleteAction(`${this.url.deleteUrl}?id=${id}`).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },

    // 创建时间改变
    timeChange(e, arr) {
      if (arr[0]) {
        this.queryParam.startTime = arr[0]
        this.queryParam.endTime = arr[1]
      } else {
        this.queryParam.startTime = undefined
        this.queryParam.endTime = undefined
      }
    }
  }
}
</script>

<style scoped></style>

